package dancrawler.server.gamecmd;

import dancrawler.common.Misc;
import dancrawler.proto.cmd.CmdHandler;
import dancrawler.server.*;
import java.io.UnsupportedEncodingException;
import java.util.Map.Entry;

/**
 * The first command a client transmits to the server Args: string proto-name,
 * short proto-version, string client-id, string player-name
 */
public class CmdHello extends CmdHandler {

    protected int[] args = new int[]{-1, 2, -1, -1};
    private GameHandler gh;

    public CmdHello(GameHandler gh) {
        this.gh = gh;;
    }

    @Override
    public void handleCmd(netMsg imsg) {
        byte[][] args;
        try {
            gh.addPlayer(imsg.getSender().getId());
            gh.changePlayerName(gh.getPlayer(imsg.getSender().getId()),
                    new String(imsg.getArgs()[0], "UTF-8"));


            Player player = gh.getPlayer(imsg.getSender().getId());
            GameObject pl = player.getPc();
            netMsg msg;
            msg = new netMsg((short) 7, (short) 0, null);
            args = new byte[1][];
            args[0] = Misc.shortToBytes((short) pl.getId());
            msg.setArgs(args);
            msg.setFinalized(true);
            imsg.getSender().getOutQueue().offer(msg);

            for (Player pl3 : gh.getPlayers()) {//Broadcast the new player
                netMsg nmsg = new netMsg((short) 14, (short) 0, null);
                args = new byte[2][];
                nmsg.setArgs(args);

                nmsg.setFinalized(true);
                args[0] = Misc.stringToBytes(player.getCon().getId());
                args[1] = Misc.shortToBytes((short) player.getId());
                pl3.getCon().getOutQueue().offer(nmsg);
                if (pl3 != player) {
                    nmsg = new netMsg((short) 14, (short) 0, null);
                    args = new byte[2][];
                    nmsg.setArgs(args);

                    args[0] = Misc.stringToBytes(pl3.getCon().getId());
                    args[1] = Misc.shortToBytes((short) pl3.getId());


                    imsg.getSender().getOutQueue().offer(nmsg);
                }
            }
            for(Entry<String,Integer> ent:gh.getTileIds())
            {
                netMsg nmsg = new netMsg((short) 21, (short) 0, null);
                args = new byte[2][];
                nmsg.setArgs(args);
                args[0] = Misc.shortToBytes((short)ent.getValue().intValue());
                args[1] = Misc.stringToBytes(ent.getKey());
                imsg.getSender().getOutQueue().offer(nmsg);
            }
            gh.sendWorld(imsg.getSender());




            msg = new netMsg((short) 6, (short) 0, null);
            args = new byte[4][];
            args[0] = Misc.shortToBytes((short) pl.getX());
            args[1] = Misc.shortToBytes((short) pl.getY());
            args[2] = Misc.stringToBytes(pl.getType());
            args[3] = Misc.shortToBytes((short) pl.getId());
            msg.setFinalized(true);
            msg.setArgs(args);
            for (Player pl2 : gh.getPlayers()) {
                Connection c = pl2.getCon();
                if (!c.getSocket().equals(player.getCon().getSocket())) {
                    c.getOutQueue().offer(msg);
                }
            }

        } catch (UnsupportedEncodingException ex) {
            System.exit(-1);//If the system lacks support for UTF-8 there is really no point in living.
        }
    }

    @Override
    public int getNumArgs() {
        return args.length;
    }

    @Override
    public int getId() {
        return 13;
    }

    @Override
    public int getArgSize(int n) {
        if (n < 0 || n >= args.length) {
            return 0;
        }
        return args[n];
    }

    @Override
    public CmdHandler getHandler() {
        return this;
    }
}
